/*
 * Joystick.h
 *
 * Author: Samuel Queiroz
 */

#ifndef JOYSTICK_H_
#define JOYSTICK_H_

#include "../Component/Button.h"
#include "../Thread/Runnable.h"

#include "SDL/SDL.h"

#define MAXIMUM_VALUE 32768
#define ZERO_PERCENTAGE 0.25
#define SPEED_UNITS 20
#define ROTATION_UNITS 10

class Joystick
	: public Runnable
{

private:

	int xMaxVirtualValue;
	int xVirtualUnitRange;

	int yMaxVirtualValue;
	int yVirtualUnitRange;

    SDL_Event *event;
	AnalogButton *leftButton;
	AnalogButton *rightButton;

	int getVirtualValueOf(int realValue, bool xAxis);

	/**
	 * Initializes a loop receiving the keyboard
	 * signals and updating the EntityManager's objects.
	 */
	void run();

	int *leftXAxis;
	int *leftYAxis;
	int *rightXAxis;
	int *rightYAxis;

public:

	Joystick(int speedUnits, int rotationUnits, SDL_Event *event, int *leftXAxis, int *leftYAxis, int *rightXAxis, int *rightYAxis)
	{
		this->leftXAxis = leftXAxis;
		this->leftYAxis = leftYAxis;
		this->rightXAxis = rightXAxis;
		this->rightYAxis = rightYAxis;

		this->event = event;

		this->leftButton = new AnalogButton();
		this->rightButton = new AnalogButton();

		int xyMaximumVirtualValue = MAXIMUM_VALUE * (1 - ZERO_PERCENTAGE);

		this->xVirtualUnitRange = xyMaximumVirtualValue / rotationUnits;
		this->xMaxVirtualValue -= xyMaximumVirtualValue % xVirtualUnitRange;

		this->yVirtualUnitRange = xyMaximumVirtualValue / speedUnits;
		this->yMaxVirtualValue -= xyMaximumVirtualValue % yVirtualUnitRange;
	}

	inline AnalogButton *getLeftButton()
	{
		return leftButton;
	}

	inline AnalogButton *getRightButton()
	{
		return rightButton;
	}

};

#endif
